package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysCorp;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysRoleUser;
import com.ruoyi.system.service.ISysCorpService;
import com.ruoyi.system.service.ISysDictTypeService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.ISysRoleUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户和角色关联Controller
 *
 * @author youde
 * @date 2022-11-16
 */
@RestController
@RequestMapping("/system/roleuser")
public class SysRoleUserController extends BaseController {
    @Autowired
    private ISysRoleUserService SysRoleUserService;

    /**
     * 查询用户和角色关联列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysRoleUser SysRoleUser) {
        List<SysRoleUser> list = SysRoleUserService.selectSysRoleUserList(SysRoleUser);
        return getDataTable(list);
    }

    /**
     * 导出用户和角色关联列表
     */
    @Log(title = "用户和角色关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysRoleUser SysRoleUser) {
        List<SysRoleUser> list = SysRoleUserService.selectSysRoleUserList(SysRoleUser);
        ExcelUtil<SysRoleUser> util = new ExcelUtil<SysRoleUser>(SysRoleUser.class);
        util.exportExcel(response, list, "用户和角色关联数据");
    }

    /**
     * 获取用户和角色关联详细信息
     */
    @GetMapping(value = "/{roleUserId}")
    public AjaxResult getInfo(@PathVariable("roleUserId") String roleUserId) {
        return AjaxResult.success(SysRoleUserService.selectSysRoleUserByRoleUserId(roleUserId));
    }

    /**
     * 新增用户和角色关联
     */
    @Log(title = "用户和角色关联", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysRoleUser[] SysRoleUser) {
        return AjaxResult.success(SysRoleUserService.insertSysRoleUser(SysRoleUser));
    }

    /**
     * 修改用户和角色关联
     */
    @Log(title = "用户和角色关联", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysRoleUser SysRoleUser) {
        return AjaxResult.success(SysRoleUserService.updateSysRoleUser(SysRoleUser));
    }

    /**
     * 删除用户和角色关联
     */
    @Log(title = "用户和角色关联", businessType = BusinessType.DELETE)
    @DeleteMapping("pkRoleUsers/{pkRoleUsers}")
    public AjaxResult remove(@PathVariable String[] pkRoleUsers) {
        SysRoleUserService.deleteSysRoleUserByIds(pkRoleUsers);
        return AjaxResult.success("删除成功");
    }

    /**
     * 删除用户和角色关联
     */
    @Log(title = "用户和角色关联", businessType = BusinessType.DELETE)
    @DeleteMapping("roleId/{roleId}")
    public AjaxResult removeAll(@PathVariable String roleId) {
        SysRoleUserService.deleteSysRoleUserByRoleId(roleId);
        return AjaxResult.success("删除成功");
    }

}